package Servlet;

import Dao.customerDao;
import Dao.customerDaoIpl;
import bean.customer;
import utils.Cookie_Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/UserServlet")
public class UserServlet extends BaseServlet{

    protected void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取is值
        Integer pid = Integer.parseInt(req.getParameter("id"));
        System.out.println(pid);
        //构造新的customer对象



        customer newcustomer = new customer(pid,req.getParameter("username"),req.getParameter("phonenumber"),req.getParameter("password"));
        //调用updatacusotomer方法，修改图书



        customerDao customerDao = new customerDaoIpl();
        customerDao.updateCustomer(newcustomer);
//
//        Cookie CookiePhoneNumber = new Cookie("phoneNumber",req.getParameter("phonenumber"));
//        CookiePhoneNumber.setMaxAge(60*60*24*365);
//        resp.addCookie(CookiePhoneNumber);
//
//        Cookie CookiePassWord = new Cookie("password",req.getParameter("password"));
//        CookiePassWord.setMaxAge(60*60*24*365);
//        resp.addCookie(CookiePassWord);
//
//
//        Cookie cookieName = new Cookie("name",req.getParameter("username"));
//        cookieName.setMaxAge(60*60*24*365);
//        resp.addCookie(cookieName);

       // cookie.setValue("new-cookie-value");  // 设置新的cookie值
//        response.addCookie(cookie);  // 重新设置cookie
        req.setAttribute("message","个人信息更改成功");

        Cookie_Session.findCookie("name", req.getCookies()).setValue(req.getParameter("username"));
        resp.addCookie(Cookie_Session.findCookie("name", req.getCookies()));
        Cookie_Session.findCookie("name", req.getCookies()).setMaxAge(60*60*24*365);

        Cookie_Session.findCookie("phoneNumber", req.getCookies()).setValue(req.getParameter("phonenumber"));
        resp.addCookie(Cookie_Session.findCookie("phoneNumber", req.getCookies()));
        Cookie_Session.findCookie("phoneNumber", req.getCookies()).setMaxAge(60*60*24*365);

        Cookie_Session.findCookie("password", req.getCookies()).setValue(req.getParameter("password"));
        resp.addCookie(Cookie_Session.findCookie("password", req.getCookies()));
        Cookie_Session.findCookie("password", req.getCookies()).setMaxAge(60*60*24*365);
        req.getRequestDispatcher("/Modify information.jsp").forward(req,resp);

    }
}
